
class Solution {
    public:
        int maxDistance(vector<vector<int>>& arrays) {
            int ans = 0, nummax = INT_MIN / 2, nummin = INT_MAX / 2;
            for (auto a : arrays) {
                ans = max({ans, nummax - a[0], a.back() - nummin});
                nummax = max(nummax, a.back());
                nummin = min(nummin, a[0]);
            }
            return ans;
        }
    };